package com.example.user1.grpc.service;


import com.example.user1.domain.UserRepository;
import com.example.user1.domain.convert.UserConvert;
import com.example.user1.infra.model.UserModel;
import com.google.protobuf.Int32Value;
import io.grpc.stub.StreamObserver;

import lombok.RequiredArgsConstructor;
import net.devh.boot.grpc.server.service.GrpcService;
import proto.user.UserInfoGrpc;
import proto.user.UserOuterClass;

@GrpcService
@RequiredArgsConstructor
public class UserInfoServiceImpl extends UserInfoGrpc.UserInfoImplBase {
    private final UserRepository userRepository;
    private final UserConvert userConvert;

    @Override
    public void get(Int32Value request, StreamObserver<UserOuterClass.User> responseObserver) {
        int id = request.getValue();
        if (id == 0) {
            responseObserver.onError(new Exception("用户不存在"));
        }
        UserModel userModel = userRepository.get((long) id);
        UserOuterClass.User user = userConvert.toUser(userModel);
        responseObserver.onNext(user);
        responseObserver.onCompleted();
    }
}